import axios from 'axios'

const instance = axios.create({
  baseURL: 'http://localhost:8181',
  timeout: 5000
})

// 请求拦截器
instance.interceptors.request.use(
  config => {
    // 可以在这里添加token等
    const token = localStorage.getItem('token')
    if (token) {
      config.headers.Authorization = `Bearer ${token}`
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 响应拦截器
instance.interceptors.response.use(
  response => {
    return response.data
  },
  error => {
    if (error.response) {
      switch (error.response.status) {
        case 401:
          console.error('未授权，请重新登录')
          break
        case 403:
          console.error('拒绝访问')
          break
        case 404:
          console.error('请求资源不存在')
          break
        case 500:
          console.error('服务器错误')
          break
      }
    }
    return Promise.reject(error)
  }
)

export const buildingApi = {
  // 获取楼宇列表（分页）
  getBuildingList(page = 1, size = 100) {
    return instance.get(`/building/list/${page}/${size}`)
  },
  
  // 搜索楼宇
  searchBuildings(params) {
    return instance.get('/building/search', { params })
  },
  
  // 删除楼宇
  deleteBuilding(id) {
    return instance.delete(`/building/deleteById/${id}`)
  },
  
  // 添加楼宇
  saveBuilding(data) {
    return instance.post('/building/save', data)
  },
  
  // 更新楼宇
  updateBuilding(data) {
    return instance.put('/building/update', data)
  }
}

export const dormitoryAdminApi = {
  // 获取宿管员列表
  getList() {
    return instance.get('/dormitoryAdmin/list')
  },
  
  // 添加宿管员
  saveAdmin(data) {
    return instance.post('/dormitoryAdmin/save', data)
  },
  
  // 删除宿管员
  deleteAdmin(id) {
    return instance.delete(`/dormitoryAdmin/deleteById/${id}`)
  }
}

export const systemApi = {
  // 系统相关API
  // ...
}